package copyengine.ui.component.tree
{
	import copyengine.ui.component.CEUISprite;
	import copyengine.ui.component.list.old.CEList;
	import copyengine.ui.component.list.old.CEListCoreEvent;
	import copyengine.ui.component.list.old.animation.ICEListAnimation;
	import copyengine.ui.component.list.old.dataprovider.CESingleRowDataProvider;
	import copyengine.ui.component.list.old.dataprovider.ICEListDataProvider;
	import copyengine.ui.component.tree.dataprovider.ICETreeCellRenderData;
	import copyengine.ui.component.tree.dataprovider.ICETreeDataProvider;
	import copyengine.utils.GeneralUtils;
	import copyengine.utils.UIUtils;
	
	import flash.display.MovieClip;

	public final class CETree extends CEUISprite
	{
		//Tree组件内部还是用List来实现
		private var treeList:CEList;

		//当前Tree结构的DataProvider
		private var treeDataProvider:ICETreeDataProvider;

		private var listDataProvider:CESingleRowDataProvider;

		//是否自动展开点击项
		public var isAutoUnfold:Boolean;

		public function CETree(_target:MovieClip,
							   _layout:String,
							   _buttonAnimation:Class,
							   _scrollBarThumBtnAnimation:Class,
							   _scrollBarTrackBtnAnimation:Class,
							   _isAutoScaleScrollBarThumb:Boolean = true,
							   _isAutoUnfold:Boolean = true)
		{
			super(_target);
			isAutoUnfold = _isAutoUnfold;
			treeList = UIUtils.getList(_target, _layout, _buttonAnimation, _scrollBarThumBtnAnimation, _scrollBarTrackBtnAnimation, _isAutoScaleScrollBarThumb);
			listDataProvider = new CESingleRowDataProvider();

			addListener();
		}

		public function initializeCETree(_cellRenderInstanceClass:Class,
										 _dataProvider:ICETreeDataProvider = null,
										 _listInteraction:ICEListAnimation = null,
										 _cellRenderAnimationClass:Class = null,
										 _listSelectableProperty:int = 1):void
		{
			treeDataProvider = _dataProvider;
			treeList.initializeCEList(_cellRenderInstanceClass, getListDataProviderByTreeProvider(), _listInteraction, _cellRenderAnimationClass, _listSelectableProperty);
		}

		public function refreshDataProvider(_dataProvider:ICETreeDataProvider = null, _isCleanScrollPosition:Boolean = false):void
		{
			treeDataProvider = _dataProvider;
			treeList.refreshDataProvider(getListDataProviderByTreeProvider(), _isCleanScrollPosition);
		}

		override protected function doDispose():void
		{
			removeListener();
			GeneralUtils.removeTargetFromParent(treeList);
			treeList = null;
		}

		/**
		 * 根据当前TreeDataprovider中的结构转化为一维ListDataProvider
		 */
		private function getListDataProviderByTreeProvider():ICEListDataProvider
		{
			listDataProvider.clear();
			if (treeDataProvider != null)
			{
				for each (var data:ICETreeCellRenderData in treeDataProvider.getData())
				{
					forEachTreeCellRenderData(data);
				}
			}
			return listDataProvider;
		}

		private function forEachTreeCellRenderData(_data:ICETreeCellRenderData):void
		{
			listDataProvider.pushData(_data);
			if (!_data.isFold && _data.getSubFoldCellRenderDataVector() != null)
			{
				for each (var subData:ICETreeCellRenderData in _data.getSubFoldCellRenderDataVector())
				{
					forEachTreeCellRenderData(subData);
				}
			}
		}

		private function addListener():void
		{
			GeneralUtils.addTargetEventListener(treeList, CEListCoreEvent.ITEM_CLICK, onClickListCellRender, true);
		}

		private function removeListener():void
		{
			GeneralUtils.removeTargetEventListener(treeList, CEListCoreEvent.ITEM_CLICK, onClickListCellRender, true);
		}

		private function onClickListCellRender(e:CEListCoreEvent):void
		{
			if (isAutoUnfold)
			{
				var data:ICETreeCellRenderData = e.clickItemData as ICETreeCellRenderData;
				data.isFold = !data.isFold;
				treeList.refreshDataProvider(getListDataProviderByTreeProvider(), false);
			}
			if (hasEventListener(CETreeEvent.ITEM_CLICK))
			{
				var event:CETreeEvent = new CETreeEvent(CETreeEvent.ITEM_CLICK);
				event.clickStageX = e.clickStageX;
				event.clickStageY = e.clickStageY;
				event.clickItemData = e.clickItemData as ICETreeCellRenderData;
				event.parameter = e.parameter;
				this.dispatchEvent(event);
				event = null;
			}
		}


	}
}
